package xyz.yuzegod.megawalls.command;

import org.bukkit.Bukkit;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.Classes;
import xyz.yuzegod.megawalls.classes.ClassesManager;
import xyz.yuzegod.megawalls.database.KeyValue;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.stats.KitStatsContainer;

public class CommandMaster extends BaseCommand
{
    public CommandMaster() {
        super("master");
        this.setPermission("MegaWalls.admin");
    }
    
    @Override
    public String getPossibleArguments() {
        return "give <玩家名> <职业> <数量>";
    }
    
    @Override
    public int getMinimumArguments() {
        return 4;
    }
    
    @Override
    public void execute(final CommandSender sender, final String label, final String[] args) throws CommandException {
        final Player player = Bukkit.getPlayer(args[1]);
        if (player == null) {
            sender.sendMessage("§c该玩家不在线！");
            return;
        }
        final Classes classes = ClassesManager.getClassesByName(args[2]);
        if (classes == null) {
            sender.sendMessage("§c该职业不存在！");
        }
        int amount = 0;
        try {
            amount = Integer.valueOf(args[3]);
        }
        catch (NumberFormatException e) {
            sender.sendMessage("§c请输入有效的数字！");
            return;
        }
        if (amount <= 0) {
            sender.sendMessage("§c请输入有效的数字！");
            return;
        }
        final GamePlayer reciver = GamePlayer.get(player.getUniqueId());
        final KitStatsContainer kitStats = reciver.getPlayerStats().getKitStats(classes);
        final int points = kitStats.getMasterPoints() + amount;
        kitStats.setMasterPoints(points);
        MegaWalls.getInstance().getDataBase().dbUpdate("classes_" + classes.getName(), new KeyValue("masterPoints", points), new KeyValue("uuid", reciver.getUuid().toString()));
        sender.sendMessage("§a给予成功！");
    }
    
    @Override
    public boolean isOnlyPlayerExecutable() {
        return false;
    }
}
